package com.hangzhou.xyj.utils;

import android.content.Context;
import android.net.Uri;

import com.mediaideas.videoengine.utils.BaseAsyncTask;
import com.mediaideas.videoengine.utils.FileUtils;

import java.io.File;
import java.util.UUID;

public class LoadAudioTask extends BaseAsyncTask<Uri, Void, String> {

    public interface Callback {
        public void onLoadAudioFinished(int audioType, String filePath);
    }

    private static final String TAG = "LoadAudioTask";
    public static final int ACCOMPANY_AUDIO_TYPE = 0x0001;
    public static final int NATIVE_AUDIO_TYPE = 0x0002;
    public static final int LYRIC_AUDIO_TYPE = 0x0003;
    private int mAudioType;
    private Callback mCallback;
    private Context mContext;

    public LoadAudioTask(Context context, Callback callback, int audioType) {
        this.mCallback = callback;
        this.mContext = context;
        this.mAudioType = audioType;
    }


    protected String doInBackground(Uri... params) {
        if (params[0] == null)
            return null;

//        int resId = params[0];
        Uri uri = params[0];
        UUID uuid = UUID.randomUUID();
        File audioFile = FileUtils.getAudioFile(mContext, uuid.toString());
        String filePath = audioFile.getAbsolutePath();
//        boolean result = FileUtils.copyFileFromResource(mContext, resId, filePath);
        boolean result = FileUtils.copyFileFromUri(mContext, uri, filePath);
        if (result) {
            return filePath;
        } else if (FileUtils.isFileExists(filePath)) {
            FileUtils.deleteFile(filePath);
            return null;
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if (mCallback != null && result != null) {
            mCallback.onLoadAudioFinished(mAudioType, result);
        }
    }
}